<template>
  <div class="permissions">
    <el-card>
      <el-row :gutter="20">
        <el-col :span="5">
          <el-input v-model="queryInfo.title" placeholder="根据用户名搜索" size="small"> </el-input>
        </el-col>
        <el-col :span="3">
          <el-button size="small" @click="clear">清除</el-button>
          <el-button type="primary" size="small" @click="protList">搜索</el-button>
        </el-col>
      </el-row>
      <!-- 添加菜单开始 -->
      <el-button type="success" class="successbtn" icon="el-icon-edit" size="mini" @click="dialogVisible = true" style="margin-top: 20px;">新增权限组</el-button>
      <!-- 添加菜单结束 -->
      <!-- 提示框区域开始 -->
      <!-- 带图标的提示 -->
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple-dark">
            <i class="el-alert__icon el-icon-info"></i>
            <span class="size">共{{ this.total }}条数据</span>
          </div>
        </el-col>
      </el-row>
      <!-- 提示框区域结束 -->
      <!-- 表格区域开始 -->
      <el-table ref="multipleTable" :data="getList" tooltip-effect="dark" style="width: 100%">
        <el-table-column type="selection" width="50px"> </el-table-column>
        <el-table-column prop="title" label="用户名" width="500"> </el-table-column>
        <!-- update_date -->
        <el-table-column prop="create_date" label="日期" width="500" sortable>
          <template v-slot:default="scope">
            {{ scope.row.create_date | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="操作" width="440">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button type="primary" plain icon="el-icon-edit" class="btn" circle @click="upDateUser(scope.row)"></el-button>
            <!-- 删除 -->
            <el-button type="danger" plain icon="el-icon-delete" class="btn" circle @click="removeUserById(scope.row.id)"></el-button>
            <!-- @click="deleUser(scope.row.id)" -->
            <!-- <span>{{scope.row}}</span> -->
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格区域结束 -->

      <!-- 分页开始 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryInfo.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="2"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 分页结束 -->
    </el-card>
    <!-- 新增权限弹出框开始 -->
    <el-dialog title="提示" @close="addFromUser" :visible.sync="dialogVisible" width="50%">
      <el-form :model="addList" :rules="addUserRules" ref="addRef">
        <el-form-item label="用户名" label-width="80px" prop="title">
          <el-input class="ipt" v-model="addList.title"></el-input>
        </el-form-item>
        <el-form-item label="权限分配" label-width="80px">
          <template>
            <el-tree :data="addjurisdictions" ref="tree" show-checkbox node-key="id" :props="addeditGetList"> </el-tree>
          </template>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addGetList">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增权限弹出框结束 -->
    <!-- 修改用户弹出成开始 -->
    <el-dialog title="提示" :visible.sync="upDateUserDialogVisible" width="50%" @close="editDialogClosed">
      <el-form :model="upDateUserList" ref="upDateRef" :rules="titRules">
        <el-form-item label="用户名" label-width="80px" prop="title">
          <el-input class="ipt" v-model="upDateUserList.title"></el-input>
        </el-form-item>
        <el-form-item label="权限分配" label-width="80px">
          <template>
            <el-tree :data="addjurisdictions" :default-checked-keys="getKey" show-checkbox node-key="id" :props="editGetList"> </el-tree>
          </template>
          <!-- :default-checked-keys -->
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="upDateUserDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="upDateUsersuccess">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户弹出层结束 -->
  </div>
</template>

<script>
import { getListUser, upDateListUser, getjurisdiction, addjurisdiction, getMenusUser, deleListUser, particulars } from '../../api/permissions/permissions'
// import { deleListUser } from '../../api/user/user'

export default {
  name: 'Permissions',
  components: {},
  props: {},
  data() {
    return {
      //  获取详情
      particularsList: [],
      // 获取key
      getKey: [],
      editGetList: {
        children: 'childs',
        label: 'title'
      },
      addeditGetList: {
        children: 'childs',
        label: 'title'
      },
      props: {
        label: 'jurisdiction.title',
        children: 'jurisdiction.children'
      },
      permission: [
        {
          id: 1,
          label: '一级 1',
          children: [
            {
              id: 4,
              label: '二级 1-1'
            },
            {
              id: 10,
              label: '三级 1-1-2'
            }
          ]
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      getList: [],
      checked: true,
      queryInfo: {
        query: '',
        page: 1,
        pagesize: 10,
        title: '',
        keyword: ''
      },
      total: 0,
      // 新增权限弹出框
      dialogVisible: false,
      // 修改用户弹出层
      upDateUserDialogVisible: false,
      // 修改用户
      upDateUserList: [],
      // 修改用户验证
      titRules: {
        title: [
          {
            required: true,
            message: '请输入登录名称',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 5,
            message: '长度在3到5个字符',
            trigger: 'blur'
          }
        ]
      },
      // 添加
      addUserRules: {
        title: [
          {
            required: true,
            message: '请输入登录名称',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 10,
            message: '长度在3到10个字符',
            trigger: 'blur'
          }
        ]
      },

      // 添加用户
      addList: {
        title: '',
        permissions: []
      },
      // 获取权限
      addjurisdictions: [],
      getjurisdictions: []
      // defaultProps: {
      //   children: 'childs',
      //   label: 'title'
      // }
    }
  },
  computed: {},
  watch: {
    // 方法一 : name function () {}
    // 方法二 : name { 1. handler () {} 此方法可加 2. immediate = true 该回调开始立刻调用 方法一没有}
  },
  created() {
    this.protList()
    // this.getMenu();
    this.getMenus()
  },
  mounted() {},
  methods: {
    // 获取用户列表
    async protList() {
      try {
        const { data } = await getListUser(this.queryInfo)
        // console.log(data);
        this.getList = data.list
        // console.log(this.getList);
        this.total = data.counts
      } catch (err) {
        console.log('获取失败')
      }
    },

    // 修改用户
    upDateUser(upUser) {
      this.upDateUserList = upUser
      // console.log(this.upDateUserList)
      this.upDateUserDialogVisible = true
      this.getjurisdiction()
      this.getMenu()
    },
    // 修改用户表单关闭验证
    editDialogClosed() {
      this.$refs.upDateRef.resetFields()
    },
    // async upDateUsersuccess() {
    //   try {
    //     const { data } = await upDateListUser(this.upDateUserList.id, {
    //       id: this.upDateUserList.id,
    //       title: this.upDateUserList.title,
    //       permissions: this.jurisdiction.permissions
    //     })
    //     console.log(data)
    //     this.$message.success('修改成功')
    //     this.upDateUserDialogVisible = false
    //     this.protList()
    //   } catch (err) {
    //     console.log('修改失败')
    //   }
    // },
    upDateUsersuccess(upDateRef) {
      this.$refs.upDateRef.validate(valid => {
        if (valid) {
          try {
            upDateListUser(this.upDateUserList.id, {
              id: this.upDateUserList.id,
              title: this.upDateUserList.title,
              permissions: this.jurisdiction.permissions
            })
            // console.log(data)
          } catch (err) {
            console.log('修改失败')
          }
          this.protList()
          this.getMenus()
          this.$message.success('修改成功')
          this.upDateUserDialogVisible = false
        } else {
          console.log('error submit!!')
          return false
        }
        this.protList()
        this.getMenus()
      })
      this.protList()
      this.getMenus()
    },
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getUserLists()
    },
    handleCurrentChange(newCurrent) {
      this.queryInfo.pagenum = newCurrent
      this.getUserLists()
    },

    addFromUser() {
      this.$refs.addRef.resetFields()
    },
    // 新增用户
    async addGetList() {
      // console.log(this.$refs.tree.getCheckedKeys())
      this.getKey = this.$refs.tree.getCheckedKeys()
      console.log(this.getKey)
      try {
        // console.log(this.addList);
        const { data } = await addjurisdiction({
          title: this.addList.title,
          permissions: this.getKey
        })
        console.log(data)
        this.getList.push(data)
        this.dialogVisible = false
        this.protList()
        console.log('添加成功')
        this.$message.success('添加成功')
      } catch (err) {
        console.log('添加失败')
      }
    },

    // 删除用户
    removeUserById(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data } = await deleListUser(id)
          console.log(data)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.protList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 获取详情
    async getMenu() {
      try {
        const { data: res } = await particulars(this.upDateUserList.id, {
          title: this.upDateUserList.title,
          permissions: ''
        })
        console.log(res)
        // console.log(res);
        // this.getjurisdictions = res;
        // this.addjurisdictions = res;
        // console.log(this.addjurisdictions);
      } catch (err) {
        console.log('获取失败')
      }
    },
    // 获取菜单
    async getMenus() {
      try {
        const { data: res } = await getMenusUser()
        // console.log(data);
        console.log(res)
        // this.getjurisdictions = res;
        this.addjurisdictions = res
        console.log(this.addjurisdictions)
        // this.$message.success('获取成功')
      } catch (err) {
        console.log('获取失败')
      }
    },

    // 获取权限分配
    async getjurisdiction() {
      const { data } = await getjurisdiction(this.upDateUserList.id, {
        id: this.upDateUserList.id,
        title: this.upDateUserList.title,
        permissions: ''
      })
      // console.log(data);
      this.jurisdiction = data
      // console.log(this.jurisdiction);
    },
    clear() {
      this.queryInfo.title = ''
      this.protList()
    }
  }
}
</script>

<style scoped lang="less">
// @import url(); 引入公共css类
.getlist {
  // width: 100%;
  header {
    margin-right: 10px;

    height: 34px;
    background-color: #fff;
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12), 0 0 3px 0 rgba(0, 0, 0, 0.04);
    margin-bottom: 30px;
  }
  .el-card {
    margin: 0 20px;
    padding: 10px, 20px;
  }
  .successbtn {
    margin-right: 10px;
    margin-bottom: 20px;
    float: right;
  }
  .btn {
    transform: translateX(-10px);
  }
  .el-alert {
    margin: 20px 0;
  }
  .el-form {
    transform: translateX(25%);
  }
  .el-tree {
    width: 300px;

    .ipt {
      width: 300px;
    }
    .el-pagination {
      margin: 20px 10px;
      float: right;
    }
  }
}
.bg-purple-dark {
  background: #f4f4f5;
}
.grid-content {
  margin-top: 20px;
  border-radius: 4px;
  min-height: 36px;
}
.el-icon-info {
  color: #aaa;
  padding-left: 15px;
  padding-top: 10px;
}
.size {
  font-size: 12px;
  padding-left: 15px;
}
.el-col-3 {
  width: 50%;
}
</style>
